package com.example.javaee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.javaee.common.result;
import com.example.javaee.entity.Asset;
import com.example.javaee.entity.LateralMovement;
import com.example.javaee.entity.VulnerabilityScan;
import com.example.javaee.service.ExploitTaskService;
import com.example.javaee.service.VulnerabilityScanService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/vulnerabilityScan")
public class VulnerabilityScanController {
    @Autowired
    private VulnerabilityScanService vulnerabilityScanService;

    //新增扫描漏洞
    @PostMapping("/add")
    public result<LateralMovement> add(HttpServletRequest request, @RequestBody VulnerabilityScan vulnerabilityScan) {

        LambdaQueryWrapper<VulnerabilityScan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VulnerabilityScan::getScanName, vulnerabilityScan.getScanName());
        VulnerabilityScan VS = vulnerabilityScanService.getOne(queryWrapper);
        if (vulnerabilityScanService.getOne(queryWrapper) != null) {
            return result.error("已存在");
        }
        vulnerabilityScanService.save(vulnerabilityScan);
        return result.success(null, "资产添加成功");
    }

    //漏洞利用列表
    @GetMapping("/getVulnerabilityScans/{pg}")
    public result<Page> page(@PathVariable Integer pg){
        //分页构造器
        Page<VulnerabilityScan> pageInfo = new Page<>(pg,12);
        //条件构造器
        LambdaQueryWrapper<VulnerabilityScan> queryWrapper = new LambdaQueryWrapper<>();
        //分页查询
        vulnerabilityScanService.page(pageInfo,queryWrapper);
        System.out.printf(String.valueOf(pageInfo));
        return result.success(pageInfo,"查询成功");

    }

    //漏洞查询
    @GetMapping("/queryByTask/{input}/{pg}")
    public result<Page> namePage(@PathVariable Integer pg, @PathVariable String input) {
        // 分页构造器
        Page<VulnerabilityScan> pageInfo = new Page<>(pg, 12);
        // 条件构造器
        LambdaQueryWrapper<VulnerabilityScan> queryWrapper = new LambdaQueryWrapper<>();
        // 如果有输入条件，则加入对name字段的筛选条件
        if (input != null && !input.isEmpty()) {
            queryWrapper.eq(VulnerabilityScan::getMyTaskId, input);
        }
        else {
            return result.error("请输入搜索内容");
        }
        // 分页查询
        vulnerabilityScanService.page(pageInfo, queryWrapper);
        System.out.printf(String.valueOf(pageInfo));
        return result.success(pageInfo, "查询成功");
    }
}
